﻿//Tzi Yong

using System;
using System.Collections.Generic;
using System.IO;

namespace Tzync
{
    /// <summary>
    /// Handles rollback for each profile
    /// </summary>
    public class RollbackSystem
    {
        private string rbLocation = GlobalVariables.Drive + GlobalVariables.ProjectName + "\\Rollback\\";  //location of rollback folder
        private long spaceAllocated = 200 * 1024 * 1024; //200 mb space on computer for storing rollback files for each profile     
        private SystemType currentSystem;  //system user is on (host or guest)
        private Profile currentProfile;    //profile in use
        private Rollback currentRollback;   //rollback in use
        private List<Rollback> rbList;  //list of rollback for each profile

        public RollbackSystem()
        {
            rbList = new List<Rollback>();
            SetupRollbackSystem();
        }

        /// <summary>
        /// Creates rollback directory
        /// </summary>
        public void SetupRollbackSystem()
        {
            try
            {
                Directory.CreateDirectory(rbLocation);  //create rollback directory
            }
            catch (UnauthorizedAccessException)
            {
                throw;
            }
        }

        public void CreateRollback()
        {
            Rollback newRollback = new Rollback(currentSystem, currentProfile, rbLocation, spaceAllocated);
            currentRollback = newRollback;
            currentRollback.CreateRollback();          
        }

        public void LoadRollback()
        {
            Rollback newRollback = new Rollback(currentSystem, currentProfile, rbLocation, spaceAllocated);
            currentRollback = newRollback;
            currentRollback.LoadRollback();
        }

        public void AddRollback(List<Difference> differenceList, List<TreeNode> arrNodes)
        {
            currentRollback.AddToRollback(differenceList, arrNodes);
        }

        /// <summary>
        /// Deletes rollback files from deleted profiles
        /// </summary>
        /// <param name="profileList">List of available profiles</param>
        public void CleanupRollback(List<String> profileList)
        {
            bool toDelete = true;
            String[] directoryList = Directory.GetDirectories(rbLocation);                
                
            foreach (String directory in directoryList) //go through each profile in rollback folder
            {
                toDelete = true;
                DirectoryInfo directoryName = new DirectoryInfo(directory);
                foreach (String profileName in profileList) //go through list of available profiles
                {
                    if (directoryName.Name.Equals(profileName))
                    {
                        toDelete = false;   //do not delete rollback of existing profile
                        break;  
                    }
                }

                if (toDelete)
                    RemoveRollback(directoryName.Name);   //delete profile in rollback folder if it is not in list of available profiles
            }
        }

        public String GetRollbackFilePath(String filePath)
        {
            return currentRollback.GetRollbackFilePath(filePath);
        }

        public void RemoveRollback(String profileName)
        {
            DirectoryInfo profileDir = new DirectoryInfo(rbLocation + @"\" + profileName);
            try
            {
                profileDir.Delete(true);
            }
            catch (IOException)
            {
                throw;
            }
        }

        public void PerformRollback(SyncTool syncTool, List<String> filesToRollback, List<TreeNode> arrNodes)
        {
            currentRollback.PerformRollback(syncTool, filesToRollback, arrNodes);
        }

        public void UpdateRollbackFile(String oldRootAddress, String newRootAddress)
        {
            currentRollback.UpdateRollbackFile(oldRootAddress, newRootAddress);
        }

        //Get and Set methods

        public long SpaceAllocated
        {
            get { return spaceAllocated; }
            set { spaceAllocated = value; }
        }

        public string RbLocation
        {
            get { return rbLocation; }
            set { rbLocation = value; }
        }

        public SystemType CurrentSystem
        {
            get { return currentSystem; }
            set { currentSystem = value; }
        }

        public Profile CurrentProfile
        {
            get { return currentProfile; }
            set { currentProfile = value; }
        }

        public Rollback CurrentRollback
        {
            get { return currentRollback; }
            set { currentRollback = value; }
        }

        public List<Rollback> RbList
        {
            get { return rbList; }
            set { rbList = value; }
        }

    }   //End of RollbackSystem class
}
